﻿namespace UnityEditor.Controls.MenuControl.ViewModel
{
    using System;

    [AttributeUsage(AttributeTargets.Method,
        AllowMultiple = false,
        Inherited = false)]
    public class MenuItemAttribute : Attribute
    {
        public const int MAXLENGTHOFMENUITEM = 3;

        public const char SEPERATOR = '/';

        private string path;

        private string[] pathNames;

        public MenuItemAttribute(string path)
        {
            Path = path;
        }

        public int Length
        {
            get
            {
                if (pathNames == null)
                {
                    return 0;
                }
                else
                {
                    if (pathNames.Length > MAXLENGTHOFMENUITEM)
                        return MAXLENGTHOFMENUITEM;
                    else
                        return pathNames.Length;
                }
            }
        }

        public string Path
        {
            set
            {
                path = value;
                pathNames = path.Split(new char[] { SEPERATOR }, StringSplitOptions.RemoveEmptyEntries);
            }

            get
            {
                return path;
            }
        }

        public string this[int index]
        {
            get
            {
                if (index < 0 || index >= pathNames.Length)
                    throw new System.Exception("Index is out of range.");
                return pathNames[index];
            }
        }
    }
}
